package customer.zspt.cn.customer.utils;

import java.io.File;
import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/8/12 0012.
 */

public class UnloadFileUtils {

    public  void onLoadFile(final String picturefile, final String type, final String url,final String token) {
        //post方式上传文件（sd卡跟路径）
        new Thread(){
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    File file = new File(picturefile);
                    RequestBody fileBody = RequestBody.create(MediaType.parse(type), file);
                    MultipartBody requestBody = new MultipartBody.Builder()
                            .setType(MultipartBody.FORM)
                            .addFormDataPart("file", file.getName(), fileBody)
                            .build();
                    Request request = new Request.Builder()
                            .url(url).
                                    addHeader("Authorization", token).
                                    addHeader("Accept", "application/json").addHeader("Content-Type", "image/jpeg").addHeader("Connection", "keep-alive")
                            .post(requestBody)
                            .build();
                    OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder();
                    httpBuilder.connectTimeout(10, java.util.concurrent.TimeUnit.SECONDS)
                            .writeTimeout(15, java.util.concurrent.TimeUnit.SECONDS)
                            .build();
                    client.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                             if(mLintener!=null){
                                 mLintener.OnFail();
                             }
                        }
                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            String result = response.body().string();
                            if(mLintener!=null){
                                mLintener.OnSuccess(result);
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                    if(mLintener!=null){
                        mLintener.OnError();
                    }
                }
            }
        }.start();
    }
    public  OnResultLintener mLintener;
    public void getResultLintener(OnResultLintener lintener){
        this.mLintener = lintener;
    }
    /**监听数据*/
     public interface OnResultLintener{
         void OnSuccess(String result);
         void OnError();
         void OnFail();
    }
}
